//
//  YYSelectedView.m
//  BagEvent-iPhone
//
//  Created by bagevent on 2018/8/23.
//  Copyright © 2018年 Allen. All rights reserved.
//

#import "YYSelectedView.h"
#import <Masonry/Masonry.h>
#define cellHeight 40

@interface YYSelectedView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, assign)UITableView *contentTableView;
@property (nonatomic, assign)UIButton *shdowBtn;
@property (nonatomic, strong)NSArray *dataAry;
@end
@implementation YYSelectedView
+(YYSelectedView *)yySelectedView:(NSArray *)dataAry selectBlock:(void(^)(NSString *selectContent))selectBlock
{
    YYSelectedView *selectedView = [[YYSelectedView alloc] init];
    selectedView.dataAry = dataAry;
    selectedView.selectBlock = selectBlock;
    [selectedView initCustomUI];
    return selectedView;
}
- (void)updateDataSource:(NSArray *)dataSource
{
    self.dataAry = dataSource;
    [self.contentTableView mas_updateConstraints:^(MASConstraintMaker *make) {
        //        if (self.dataAry.count > 4) {
        //            make.height.mas_equalTo(cellHeight * 4);
        //        }else
        //        {
        make.height.mas_equalTo(cellHeight * self.dataAry.count);
        //        }
    }];
    [self.contentTableView reloadData];
}

-(void)initCustomUI
{
    WEAKSELF
    UIButton *shadowBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:shadowBtn];
    [shadowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    _shdowBtn = shadowBtn;
    shadowBtn.backgroundColor = [UIColor blackColor];
    shadowBtn.alpha = 0.3;
    [[shadowBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf removeFromSuperview];
        if(weakSelf.selectBlock){
            weakSelf.selectBlock(nil);
        }
    }];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _contentTableView = tableView;
    [self addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.right.equalTo(self);
        make.top.equalTo(self);
//        if (self.dataAry.count > 4) {
//            make.height.mas_equalTo(cellHeight * 4);
//        }else
//        {
            make.height.mas_equalTo(cellHeight * self.dataAry.count);
//        }
    }];
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCells"];
    tableView.delegate = self;
    tableView.dataSource = self;
    
    
}

- (void)updateConstranit:(BOOL)up
{
    if (up) {
        [_contentTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.top.equalTo(self);
            make.height.mas_equalTo(cellHeight * self.dataAry.count);
        }];
    }else{
        [_contentTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.bottom.equalTo(self);
            make.height.mas_equalTo(cellHeight * self.dataAry.count);
        }];
    }
}


#pragma mark -delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataAry.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCells" forIndexPath:indexPath];
    [cell.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    NSDictionary *dic = self.dataAry[indexPath.row];

    // 选中图标
    UIImageView *imgView = [[UIImageView alloc] init];
    [cell addSubview:imgView];
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.centerY.equalTo(cell);
        make.size.mas_equalTo(CGSizeMake(15, 15));
    }];
    imgView.image = [UIImage imageNamed:[dic valueForKey:@"imgName"]];
    
    // 名字
    UILabel *nameLab = [[UILabel alloc] init];
    [cell addSubview:nameLab];
    [nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imgView.mas_right).offset(14);
        make.centerY.equalTo(cell);
    }];
    nameLab.font = [UIFont systemFontOfSize:14];
    if ([[dic valueForKey:@"title"] isEqualToString:_defaultStr]) {
        nameLab.textColor = YYRGB(227, 107, 99);
    }else{
        nameLab.textColor = YYRGB(83, 83, 83);
    }
    nameLab.text =[dic valueForKey:@"title"];
    
    
    // 分割线
    UIView *line = [[UIView alloc] init];
    [cell addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.right.offset(0);
        make.bottom.equalTo(cell);
        make.height.mas_equalTo(1);
    }];
    line.backgroundColor = YYRGB(241, 241, 241);
    
    line.hidden = NO;    
    return cell;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return cellHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self removeFromSuperview];
    NSDictionary *str = self.dataAry[indexPath.row];
    if(self.selectBlock){
        self.selectBlock([str valueForKey:@"title"]);
    }
}

//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 50;
//}

//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UIView *view = [[UIView alloc] init];
//    view.backgroundColor = [UIColor whiteColor];
//    // 名字
//    UILabel *nameLab = [[UILabel alloc] init];
//    [view addSubview:nameLab];
//    [nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(view).offset(14);
//        make.centerY.equalTo(view);
//    }];
//    nameLab.font = [UIFont boldSystemFontOfSize:15];
//    nameLab.textColor = [UIColor colorWithHexCode:@"979898"];
//    nameLab.text = @"我的活动";
//    return view;
//}

@end
